package yxy.game.pm2.command.pack;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.item;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.conquest.ConquestHandle;
import yxy.game.pm2.module.conquest.ConquestModule;

/**
 * 背包 即时礼包物品展示
 */
@MessageType(proto = Command.ItemDetail, classtype = PackageMessage.ItemDetailRequest.class)
public class CMD_ItemDetail extends Command<PackageMessage.ItemDetailRequest> {

    @Override
    public void execute(MessageSource messageSource, PackageMessage.ItemDetailRequest request) {

        try {
            int dataId = request.getId();
            String playerCode = messageSource.getTag();
            PackageMessage.ItemDetailResponse.Builder response = PackageMessage.ItemDetailResponse.newBuilder();

            item data = DataCenter.getData(dataId, item.class);
            String gtype = data.getGtype();
            String[] str = gtype.split("_");
            //银币    str[0]:类型  str[1]:小时
            if (str[0].equals("2")) {
                getSilver(playerCode, response, str[1]);
            }
            //经验
            if (str[0].equals("3")) {
                getExperience(playerCode, response, str[1]);
            }
            //将星玉
            if (str[0].equals("5")) {
                getJade(playerCode, response, str[1]);
            }

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void send(MessageSource messageSource, PackageMessage.ItemDetailResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


    //根据时间获取银币
    private void getSilver(String playerCode, PackageMessage.ItemDetailResponse.Builder response, String time) {
        try {
            long passTime = Long.parseLong(time) * 60 * 1000;
            ConquestModule conquestModule = ConquestModule.get(playerCode);
            Currencies rewards = ConquestHandle.holdRewardOnlyCoin(conquestModule.getMapId(), passTime);
            response.addAllReward(new CurrencyHandle().packItems(rewards));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //根据时间获取经验
    private void getExperience(String playerCode, PackageMessage.ItemDetailResponse.Builder response, String time) {
        try {
            long passTime = Long.parseLong(time) * 60 * 1000;
            ConquestModule conquestModule = ConquestModule.get(playerCode);
            Currencies rewards = ConquestHandle.holdRewardOnlyHeroexp(conquestModule.getMapId(), passTime);
            response.addAllReward(new CurrencyHandle().packItems(rewards));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //根据时间获取将星玉
    private void getJade(String playerCode, PackageMessage.ItemDetailResponse.Builder response, String time) {
        try {
            long passTime = Long.parseLong(time) * 60 * 1000;
            ConquestModule conquestModule = ConquestModule.get(playerCode);
            Currencies rewards = ConquestHandle.sweepRewardOnlyJade(conquestModule.getMapId(), passTime);
            response.addAllReward(new CurrencyHandle().packItems(rewards));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}